package com.ruoyi.legal.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.legal.domain.Xstz;
import com.ruoyi.legal.domain.vo.XstzBatchVo;
import com.ruoyi.legal.domain.vo.YstzBatchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.legal.mapper.YstzMapper;
import com.ruoyi.legal.domain.Ystz;
import com.ruoyi.legal.service.IYstzService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 移送台账Service业务层处理
 *
 * @author xwj
 * @date 2025-04-22
 */
@Service
public class YstzServiceImpl extends ServiceImpl<YstzMapper, Ystz> implements IYstzService {
    @Autowired
    private YstzMapper ystzMapper;

    /**
     * 查询移送台账
     *
     * @param id 移送台账主键
     * @return 移送台账
     */
    @Override
    public Ystz selectYstzById(Long id) {
        return ystzMapper.selectYstzById(id);
    }

    /**
     * 查询移送台账列表
     *
     * @param ystz 移送台账
     * @return 移送台账
     */
    @Override
    public List<Ystz> selectYstzList(Ystz ystz) {
        return ystzMapper.selectYstzList(ystz);
    }

    /**
     * 新增移送台账
     *
     * @param ystz 移送台账
     * @return 结果
     */
    @Override
    public Ystz insertYstz(Ystz ystz) {

        //  ystzMapper.insertYstz(ystz);
        save(ystz);
        return ystz;
    }

    /**
     * 修改移送台账
     *
     * @param ystz 移送台账
     * @return 结果
     */
    @Override
    public Ystz updateYstz(Ystz ystz) {

        //  ystzMapper.updateYstz(ystz);
        updateById(ystz);
        return ystz;
    }

    /**
     * 批量删除移送台账
     *
     * @param ids 需要删除的移送台账主键
     * @return 结果
     */
    @Override
    public int deleteYstzByIds(Long[] ids) {
        return ystzMapper.deleteYstzByIds(ids);
    }

    /**
     * 条件删除
     * @param ystz 移送台账
     * @return
     */
    @Override
    public int delete(Ystz ystz) {
        QueryWrapper<Ystz> qw = new QueryWrapper<>();
        QueryTool.condition(qw, ystz);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param ystz 移送台账
     * @return
     */
    @Override
    public Pages<Ystz> queryPage(Ystz ystz) {
        QueryWrapper<Ystz> qw = new QueryWrapper<>();
        QueryTool.condition(qw, ystz);
        IPage<Ystz> ipage = new Query<Ystz>().getPage(ystz);

        IPage<Ystz> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    @Override
    @Transactional
    public void batchSaveXstzBatch(YstzBatchVo ystzBatchVo, SysUser user) {
        List<Ystz> ystzList = ystzBatchVo.getList();
        List<Long> ids = ystzBatchVo.getDeleteIds();

        Date now = new Date();
        String nowStr = DateUtil.formatDateTime(DateUtil.date());

        for(Ystz ystz: ystzList) {
            if(ystz.getId() == null) {
                ystz.setStatus("0");
                ystz.setCreateId(user.getUserId());
                ystz.setCreateBy(user.getNickName());
                ystz.setCreateTime(now);
            } else {
                ystz.setUpdateId(user.getUserId());
                ystz.setUpdateTime(DateUtils.getNowDate());
                ystz.setUpdateBy(user.getNickName());
            }
        }

        saveOrUpdateBatch(ystzList);
        if(ids != null && ids.size() > 0) {
            deleteYstzByIds(ids.toArray(new Long[0]));
        }

    }

    /**
     * 删除移送台账信息
     *
     * @param id 移送台账主键
     * @return 结果
     */
    @Override
    public int deleteYstzById(Long id) {
        return ystzMapper.deleteYstzById(id);
    }
}
